var t = getApp();

Page({
    data: {
        agreement: !1,
        promiseRadio: "1",
        addressName: "选择地区",
        addressShow: !1,
        provinceData: [],
        cityData: [],
        districtData: [],
        pickerValue: [ 0, 0, 0 ],
        selectAddressData: [],
        districtVal: [],
        districtFieldShow: !1,
        districtPopupShow: !1,
        provice: "",
        city: "",
        district: "",
        street: "选择街道",
        applyModel: {
            provinceId: "",
            cityId: "",
            districtId: "",
            streetId: ""
        },
        th_name: "",
        th_pass: "",
        th_mobile: "",
        th_address: "",
        promiseText: "输入提货密码查询",
        configInfo: {},
        goods_id: 0,
        goods_option_id: 0,
        th_pass_flag: !1,
        pluginOptions: {
            plugin_name: "旅游激活卡",
            password_name: "激活密码",
            city_name: "出发城市",
            address_name: "出发地址"
        },
        plugin: "picking-card"
    },
    onLoad: function(a) {
        var e = [];
        if (a.scene) {
            var i = decodeURIComponent(a.scene);
            if (i) {
                e = i.split(",");
                for (var s = 0; s < e.length; s++) {
                    var d;
                    "mid" == (d = e[s].split("="))[0] && t._setMid(d[1]);
                }
            }
        }
        if (a.plugin && "one" == a.plugin || -1 !== e.indexOf("plugin=one")) {
            wx.setNavigationBarTitle({
                title: "提货卡"
            });
            this.setData({
                pluginOptions: {
                    plugin_name: "提货卡",
                    password_name: "提货密码",
                    city_name: "所在城市",
                    address_name: "收货地址"
                },
                plugin: "picking-card-one"
            });
        } else wx.setNavigationBarTitle({
            title: "旅游激活卡"
        });
        this.getConfigInfo(), this._initAddressInfo();
    },
    addressShowbtn: function() {
        this.setData({
            addressShow: !0
        });
    },
    getConfigInfo: function() {
        var a = this, e = "plugin.picking-card.frontend.pickingCard.getConfig";
        "picking-card-one" == this.data.plugin && (e = "plugin.picking-card-one.frontend.pickingCard.getConfig");
        var i = t.getNetAddresss(e);
        t._getNetWork({
            url: i,
            success: function(t) {
                var e = t.data;
                1 == e.result && (e.data.is_show = Number(e.data.is_show), a.setData({
                    configInfo: e.data
                }));
            }
        });
    },
    selectPromisePrice: function() {
        var a = this, e = "plugin.picking-card.frontend.pickingCard.getPrice";
        "picking-card-one" == this.data.plugin && (e = "plugin.picking-card-one.frontend.pickingCard.getPrice");
        var i = t.getNetAddresss(e);
        t._getNetWork({
            url: i,
            data: {
                password: this.data.th_pass
            },
            success: function(t) {
                var e = t.data;
                if (1 != e.result) return console.log(e), a.setData({
                    th_pass_flag: !1
                }), a.tips(e.msg);
                a.setData({
                    promiseText: e.data.margin,
                    th_pass_flag: !0,
                    goods_id: e.data.goods_id,
                    goods_option_id: e.data.goods_option_id
                });
            }
        });
    },
    send: function() {
        var a = this, e = this.data, i = e.th_pass_flag, s = e.th_pass, d = e.th_name, n = e.th_mobile, r = e.districtFieldShow, o = e.applyModel, c = e.provice, l = e.city, p = e.district, g = e.street, u = e.th_address, h = e.promiseText, f = e.promiseRadio, _ = e.goods_id, m = e.goods_option_id;
        if ("" == s) return this.tips("提货密码不能为空");
        if (!i) return this.tips("提货密码错误");
        if ("" == d) return this.tips("姓名不能为空");
        if ("" == n) return this.tips("手机号不能为空");
        if (!o.provinceId || !o.cityId || !o.districtId) return this.tips("请选择地区");
        if (1 == r && !o.streetId) return this.tips("请选择街道");
        if ("" == u) return this.tips("收货地址不能为空");
        if ("picking-card-one" == this.data.plugin && 0 == this.data.promiseRadio) return this.tips("请选择是否使用".concat(this.data.configInfo.commitment, "兑换滤芯"));
        if ("picking-card-one" == this.data.plugin && !this.data.agreement) return this.tips("请勾选提货说明");
        console.log("发送请求");
        var D = {
            username: d,
            mobile: n,
            address: u,
            province: c,
            city: l,
            district: p
        };
        1 == r && (D.street = g);
        var v = "plugin.picking-card.frontend.create.index";
        "picking-card-one" == this.data.plugin && (v = "plugin.picking-card-one.frontend.create.index");
        var k = t.getNetAddresss(v);
        t._postNetWork({
            url: k,
            data: {
                dispatch_type_id: 1,
                address: JSON.stringify(D),
                goods: JSON.stringify([ {
                    goods_id: _,
                    total: 1,
                    option_id: m
                } ]),
                price: h,
                password: this.data.th_pass,
                is_filter: Number(f) - 1
            },
            success: function(t) {
                var e = t.data;
                if (1 != e.result) return console.log(e), a.tips(e.msg);
                wx.redirectTo({
                    url: "/packageD/buy/orderPay/orderPay?order_id=" + e.data.order_ids + "&status=2"
                });
            }
        });
    },
    onReady: function() {},
    onShow: function() {},
    onChange: function(t) {
        this.setData({
            agreement: t.detail
        });
    },
    onChangePromiseRadio: function(t) {
        this.setData({
            promiseRadio: t.detail
        });
    },
    onHide: function() {},
    onUnload: function() {},
    streetShowbtn: function() {
        this.setData({
            districtPopupShow: !0
        });
    },
    treetShowLeft: function() {
        this.setData({
            districtPopupShow: !1
        });
    },
    streetConfirm: function(t) {
        var a = t.currentTarget.dataset.item;
        this.setData({
            street: a.areaname,
            "applyModel.streetId": a.id,
            districtPopupShow: !1
        });
    },
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    setExplainFlag: function() {
        this.setData({
            explainFlag: !this.data.explainFlag
        });
    },
    onShareAppMessage: function() {},
    _initAddressInfo: function() {
        var a = this;
        wx.getStorage({
            key: "provinceData",
            success: function(t) {
                console.log("地址信息准备就绪"), a._getProvinceData();
            },
            fail: function(e) {
                console.log("省fail"), t._getNetWork({
                    url: t.getNetAddresss("member.member-address.address"),
                    success: function(t) {
                        var e = t.data, i = e.data.province, s = e.data.city, d = e.data.district;
                        wx.setStorage({
                            key: "provinceData",
                            data: i
                        }), wx.setStorage({
                            key: "cityData",
                            data: s
                        }), wx.setStorage({
                            key: "districtData",
                            data: d
                        }), a._initAddressInfo();
                    },
                    fail: function(t) {},
                    state: !0
                });
            }
        });
    },
    _getProvinceData: function() {
        var t = this;
        wx.getStorage({
            key: "provinceData",
            success: function(a) {
                console.log("省读取成功", a.data), t.setData({
                    provinceData: a.data
                }), t.setData({
                    "selectAddressData[0].name": a.data[t.data.pickerValue[0]].areaname,
                    "selectAddressData[0].id": a.data[t.data.pickerValue[0]].id,
                    "selectAddressData[0].index": t.data.pickerValue[0]
                }), t._getCityData();
            }
        });
    },
    _getCityData: function() {
        var t = this.data.selectAddressData[0];
        console.log("城市", "_data", t);
        var a = this;
        wx.getStorage({
            key: "cityData",
            success: function(e) {
                console.log("城市读取成功", e);
                for (var i = [], s = 0; s < e.data.length; s++) e.data[s].parentid == t.id && i.push(e.data[s]);
                console.log("城市数据筛选", i), a.setData({
                    cityData: i
                }), a.setData({
                    "selectAddressData[1].name": i[a.data.pickerValue[1]].areaname,
                    "selectAddressData[1].id": i[a.data.pickerValue[1]].id,
                    "selectAddressData[1].index": a.data.pickerValue[1]
                }), a._getDistrictData();
            }
        });
    },
    _getDistrictData: function() {
        var t = this.data.selectAddressData[1];
        console.log("地区", "_data", t), console.log(t);
        var a = this;
        wx.getStorage({
            key: "districtData",
            success: function(e) {
                console.log("地区读取成功");
                for (var i = [], s = 0; s < e.data.length; s++) e.data[s].parentid == t.id && i.push(e.data[s]);
                console.log("地区数据筛选", i), a.setData({
                    districtData: i
                }), console.log("districtData", i), a.setData({
                    "selectAddressData[2].name": i[a.data.pickerValue[2]].areaname,
                    "selectAddressData[2].id": i[a.data.pickerValue[2]].id,
                    "selectAddressData[2].index": a.data.pickerValue[2]
                }), console.log("over", a.data.selectAddressData);
            }
        });
    },
    bindChange: function(t) {
        var a = t.detail.value;
        this.data.pickerValue[0] != a[0] && (a[1] = 0, a[2] = 0), this.data.pickerValue[1] != a[1] && (a[2] = 0), 
        this.setData({
            "pickerValue[0]": a[0],
            "pickerValue[1]": a[1],
            "pickerValue[2]": a[2]
        }), this._initAddressInfo();
    },
    resultAdd: function() {
        var t = this.data.selectAddressData, a = t[0].name + " " + t[1].name + " " + t[2].name;
        this.setData({
            addressName: a,
            provice: t[0].name,
            city: t[1].name,
            district: t[2].name
        }), this.setData({
            "applyModel.provinceId": t[0].id,
            "applyModel.cityId": t[1].id,
            "applyModel.districtId": t[2].id,
            addressShow: !1
        }), this.getStreet(t[2].id);
    },
    getStreet: function(a) {
        var e = this, i = "";
        i += t.getNetAddresss("member.memberAddress.street"), i += "&district_id=" + a, 
        t._getNetWork({
            url: i,
            success: function(a) {
                var i = a.data;
                1 == i.result && !t._isTextEmpty(i.data) && i.data.length > 0 ? e.setData({
                    districtVal: i.data,
                    districtFieldShow: !0
                }) : e.setData({
                    districtFieldShow: !1
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    _closeDateLw: function() {
        this.setData({
            addressShow: !1
        });
    },
    tips: function(t) {
        return wx.showToast({
            icon: "none",
            title: t
        }), !1;
    }
});